﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WheaterMapParsingTests
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.WindowStyle = WindowStyle.None;
        }

        private async void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            //var client = new HttpClient();
            //var uri = new Uri("http://darkskysatellite.s3.amazonaws.com/215159013250/1369418400.png?1369437942");

            //byte[] br = await client.GetByteArrayAsync(uri);
            //Stream respStream = await client.GetStreamAsync(uri);
            //PngBitmapDecoder decoder = new PngBitmapDecoder(respStream, BitmapCreateOptions.IgnoreColorProfile, BitmapCacheOption.Default);
            //BitmapSource bitmapSource = decoder.Frames[0];

            //WriteableBitmap newwritablebitmap = new WriteableBitmap(500, 500, 96, 96, PixelFormats.Pbgra32, null);
            //WriteableBitmap newwritablebitmap2 = new WriteableBitmap(500, 500, 96, 96, PixelFormats.Pbgra32, null);
            ////   WriteableBitmap newwritablebitmap = new WriteableBitmap(bitmapSource);
            //WriteableBitmap newrbmp = new WriteableBitmap(bitmapSource);

            //int h = newrbmp.PixelHeight;
            //int w = newrbmp.PixelWidth;
            //int[] pixelData = new int[w * h];
            //int[] pixelData2 = new int[w * h];
            //int[] pixelData3 = new int[w * h];
            //int widthInByte = 4 * w;

            //newwritablebitmap.CopyPixels(pixelData, widthInByte, 0);
            //newrbmp.CopyPixels(pixelData2, 4* w, 0);
            //newwritablebitmap2.CopyPixels(pixelData3, widthInByte, 0);

            //int[] ttt = new int[] { 61494528, 111826176, 179463680, 246831890, 314073870, 397501206, 481715218, 582248726, 666331155, 767257366, 867789076, 968321814, 1069181204, 1186556181, 1287154452, 1404594965, 1522232086, 1639607061, 1757047574, 1874488086, 1975085589, 2092526102, -2084934891, -1967494378, -1850053866, -1732613355, -1615172842, -1514444267, -1413846250, -1313248747, -1212519658, -1111856362, -1011127530, -927306986, -843421162, -759469547, -675648746, -608539882, -541431018, -491099370, -440833514, -423990504, -424056037, -424056035, -424121568, -424121565, -424252634, -424252630, -424252626, -424252621, -424252618, -424318150, -424383682, -424383678, -424449210, -424449204, -424514736, -424514731, -424580262, -424645792, -424645788, -424711318, -424776849, -424776844, -424907911, -424907905, -424973437, -424973430, -425038961, -425104492, -425170023, -425235553, -425301084, -425366615, -425366610, -425432141, -425563208, -425563203, -425628734, -425759802, -425825332, -425890865, -425956396, -426021928, -426087461, -426152993, -426284061, -426349594, -426480663, -426546195, -426611729, -426742798, -426808077, -426938890, -427069448, -427134982, -427330820, -427461377, -427591937, -427788033, -427918849, -428114689, -428244993, -428441089, -428637185, -428898561, -429028865, -429290241, -429486081, -429681921, -429877761, -430073345, -430334977, -430530817, -430726657, -430922497, -431118337, -431379713, -431575809, -431771393, -431966977, -432228609, -432358657, -432554497, -432684801, -432815105, -433010945, -433206785, -433337089, -433467393, -433597697, -433728001, -433793025, -433923329, -433988353, -434053121, -434052609, -434052097, -434051585, -434050817, -434050561, -434050049, -434049537, -434049281, -433983233, -433917441, -433785857, -433654785, -433457665, -433260801, -433063937, -432801537, -432604673, -432342273, -432080129, -431752193, -431424257, -431162113, -430899713, -430440961, -430113281, -429785089, -429391873, -428998657, -428605441, -428277761, -427884545, -427556865, -427098113, -426639361, -426246145, -425852929, -425525249, -425066497, -424738817, -424345601, -423952385, -423559169, -423165953, -422772737, -422510593, -422182913, -421855233, -421527553, -421199873, -420937729, -420675585, -420413441, -420216833, -419954689, -419758081, -419627009, -419430401, -419430401, -419430401, -419430401, -419430401, -419430401, -419430401, -419430401, -419430401, -419430401, -419430401, -419430401, -419430401, -419430401, -419430401, -419430401, -419430401, -419430401, -419430401, -419430401, -419430401, -419430401, -419430401, -419430401, -419430401, -419430401, -419759361, -420022529, -420285697, -420548865, -420943617, -421272577, -421667329, -422062081, -422522625, -422917377, -423377921, -423838465, -424299009, -424891137, -425285889, -425812225, -426338561, -426930689, -427391233, -427851777, -428443905, -428904705, -429430785, -429957121, -430483457, -431009793, -431470337, -431930881, -432391425, -432851969, -433312513, -433707265, -434102017, -434496769, -434957313, -435220481, -435483649, -435812609 };
            //int[] ttt2 = new int[] { -84282550, -84283061, -84218804, -84219571, -84220591, -84287149, -84288428, -84223913, -84225446, -84226724, -84293792, -84230045, -84231321, -84232599, -101011093, -101012370, -101013902, -101080715, -117858951, -134637445, -134638211, -151612799, -168390267, -185168248, -185365363, -185366128, -185694062, -185956202, -203191909, -203716194, -204109406, -204764507, -205288792, -206009682, -206599504, -223900491, -224555848, -225210948, -242643519, -243363900, -243887927, -244542516, -261843761, -262301999, -263022126, -263349548, -263742506, -281044008, -281371176, -298148134, -298344486, -298344485, -315121444, -315121188, -315120932, -331897891, -331897634, -332028706, -348805409, -348805152, -365582367, -365647647, -365647647, -382424350, -382424094, -399200796, -399200796, -415977755, -415911963, -432754201, -432753689, -449399576, -449399319, -466241558, -466241558, -466241558, -466241558, -466241558, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            
            //List<double> trek = new List<double>();
            //trek.Add(0);
            //for (int i = 0; i < ttt2.Length; i++)
            //{
            //    string tttttt = ttt2[i].ToString("X");
            //    pixelData3[i] = ttt2[i];
            //}

            //for (int i = 0; i < pixelData2.Length; i++)
            //{
            //    byte[] bytes = BitConverter.GetBytes(pixelData2[i]);
            //    if ((new byte[] { bytes[0], bytes[2], bytes[1] }).Average(x => x) > trek.Max()) trek.Add((new byte[] { bytes[0], bytes[2], bytes[1] }).Average(x => x));
            //}

            //double maxv = trek.Last();

            //for (int i = 0; i < pixelData.Length; i++)
            //{

            //    if (pixelData2[i] != -16777216)
            //    {

            //        //string t = pixelData2[i].ToString("X");
            //        byte[] bytes = BitConverter.GetBytes(pixelData2[i]);
            //     //   byte[] bytes2 = BitConverter.GetBytes(ttt2[(110 - (int)((new byte[] { bytes[0], bytes[1], bytes[2] }).Average(x => x)))]);

            //     //   pixelData[i] = BitConverter.ToInt32(new byte[] { bytes2[0], bytes2[1], bytes2[2], (byte)(((bytes2.Where(x => x < 255).Average(x => x)) / 255) * 200) }, 0);

            //        int t = (88 -(int)(88 *( (((new byte[] { bytes[0], bytes[1], bytes[2] }).Average(x => x)))/ maxv)));
                   
            //            pixelData3[i] = ttt2[t];
            
            //        //bytes[0] = 0xC8;
            //        //byte b = bytes[3];
            //        //bytes[3] = bytes[1];
            //        //bytes[1] = b;
            //        //if (bytes[0] < 10 && bytes[1] < 10 && bytes[2] < 10)
            //        //{
            //        //    pixelData[i] = 0;
            //        //}
            //        //else if (bytes[0] < 20 && bytes[1] < 20 && bytes[2] < 20)
            //        //{
            //        //    pixelData[i] = BitConverter.ToInt32(new byte[] { 0xff, 0x7a, 0x9a, 0xab}, 0);
            //        //  //  66a8ff
            //        //}
            //        //else if (bytes[0] < 30 && bytes[1] < 30 && bytes[2] < 30)
            //        //{
            //        //    pixelData[i] = BitConverter.ToInt32(new byte[] { 0xff, 0xc0, 0x76, 0xb6 }, 0);
            //        //}
            //        //else
            //        //{
            //        //    string tht = pixelData2[i].ToString("X");
            //        //    int tht2 = pixelData2[i];
            //        //    pixelData[i] = BitConverter.ToInt32(new byte[] { bytes[0], bytes[2], bytes[1], (byte)(((bytes.Where(x => x < 255).Average(x => x)) / 255) * 200) }, 0);
            //        //}
            //        //  pixelData[i] = (int)(pixelData[i] ^ 0xffffffff);
            //       if ((new byte[] { bytes[0], bytes[2], bytes[1] }).Average(x => x) > trek.Max()) trek.Add((new byte[] { bytes[0], bytes[2], bytes[1] }).Average(x => x));
            //   }
            //    else
            //    {
            //        pixelData[i] = 0;
            //    }

             

            //}

            //  newwritablebitmap.WritePixels(new Int32Rect(0, 0, w, h), pixelData, widthInByte, 0);
            //  newwritablebitmap2.WritePixels(new Int32Rect(0, 0, w, h), pixelData3, widthInByte, 0);
            //Map.Source = newwritablebitmap;
            //Map2.Source = newwritablebitmap2;
            //Cmap.Source = newrbmp;
            ////try
            ////{
            //////    string t = (br[4 * i + 3] << 24 | br[4 * i + 2] << 16 | br[4 * i + 1] << 8 | br[4 * i]).ToString("X");
            //// //   pixelData[i]= br[4 * i + 3] << 24 | br[4 * i + 2] << 16 | br[4 * i + 1] << 8 | br[4 * i];
            ////    pixelData[i] = 0xff << 24 | br[3 * i + 2] | br[3 * i + 1] << 8 | br[3 * i] <<16;
            ////}
            ////catch
            ////{
            ////    pixelData[i] = 0;
            ////}





            //using (FileStream stream5 = new FileStream("Test5.png", FileMode.Create))
            //{
            //    PngBitmapEncoder encoder5 = new PngBitmapEncoder();
            //    encoder5.Frames.Add(BitmapFrame.Create(newwritablebitmap2));
            //    encoder5.Save(stream5);
            //    stream5.Close();
            //}

            //BitmapImage img = new BitmapImage(new Uri("Test5.1.png", UriKind.Relative));
            //WriteableBitmap wbm = new WriteableBitmap(img);
            //int hh = wbm.PixelHeight;
            //int ww = wbm.PixelWidth;
            //int[] pixelData4 = new int[ww * hh];
            //int widthInByte4 = 4 * ww;

            //wbm.CopyPixels(pixelData4, widthInByte4, 0);
            //string teet = "";
            //for (int i = 0; i < 250; i++)
            //{
            //    teet += pixelData4[i] + ", ";
            //}
            //File.WriteAllText("values.txt", teet);

            var ewrwe = UnixTimeStampToDateTime(1369564840);
        }

        public static DateTime UnixTimeStampToDateTime(double unixTimeStamp)
        {
            // Unix timestamp is seconds past epoch
            System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToLocalTime();
            return dtDateTime;
        }
    }


}
